<?php 
/**
 * 前台内容页控制器
 */
class ShowController extends CommonController{
	public function index(){
		// 共用导航分类
    	$this->category();  
    	// 通过get获得商品id,类型id
    	$gid = Q('get.gid',0,'intval');
    	$tid = Q('get.tid',0,'intval');
    	$g_data = K("Goods")->where("gid='{$gid}'")->find();
    	// p($g_data);
    	$g_cid = $g_data['category_cid'];
    	// 点击数加一
    	K("Goods")->where("gid='{$gid}'")->update(array('click'=>$g_data['click'] +=1));
    	$this->assign('g_data',$g_data);  

    	$g_model = K("GoodsAttr");
    	$where_g = "goods_gid = {$gid}";
		//1.根据tid,class=1找到类型属性表的所有规格数组*********************
		$ta_spec_data = K('TypeAttr')->where("class=1 AND type_tid={$tid}")->all();
		//根据商品ID和循环规格数组得到类型属性ID查找商品属性表得到商品属性ID和商品属性值
		foreach ($ta_spec_data as $key => $value) { 
			$ta_spec_data[$key]['spec'] = $g_model->where($where_g . " AND type_attr_taid = {$value['taid']}")->order('gaid')->getField("gaid,gavalue",true);
		} 
		$this->assign('ta_spec_data',$ta_spec_data);
		// p($ta_spec_data);
		//2.根据商品ID，获得货品列表的所有数组******************************************
		$gl_data = K('GoodsList')->where($where_g)->all();
		// 循环货品列表数组，查找商品属性表将商品属性id，商品属性值压入列表数组中
		foreach ($gl_data as $key => $value) { 
			$gl_data[$key]['com'] = $g_model->where("gaid IN ({$value['combline']})")->getField("gaid,gavalue",true);
		}
		$this->assign('gl_data',$gl_data);
		// p($gl_data);
		// 查询商品详细表
		$gd_data = K('GoodsDetail')->where("goods_gid='{$gid}'")->find();
		$gd_data['small'] = explode(',',$gd_data['small']);
		$gd_data['medium'] = explode(',',$gd_data['medium']);
		// p($gd_data);
		$this->assign('gd_data',$gd_data);
		// 云台热卖
		$this->hot();
		// 当前分类位置
		$cid = M('goods')->where("gid='{$gid}'")->getField('category_cid');
        $path = $this->path($cid);
        ksort($path);
        // p($path);
        $this->assign('path',$path);
        // 分类名
        $this->assign('cname_data',$path[$g_cid]);

        
        
		$this->display();
	}
	/**
	 * 异步查询不同规格的货品
	 */
	public function getGoodsList(){
		// p(Q('post.'));
		$combline = Q('post.combline'); 
		$combline = isset($combline) ? $combline : '';
		// p($combline);
		$combline_arr = explode(',',$combline);
		// p($combline_arr);
		if ($combline_arr[1] == 'undefined') {
			$this->ajax(
				array(
					'stat' => 0,
					'info' => '请选择尺码规格'
					));
			die;
		}
		if ($combline_arr[0] == $combline_arr[1]) {
			$this->ajax(
				array(
					'stat' => 0,
					'info' => '请选择颜色规格'
					));
			die;
		}
		if ($combline == '') {
			$this->ajax(
				array(
					'stat' => 0,
					'info' => '非法请求'
					));
			die;
		}
		$gl_data = K("GoodsList")->where("combline='{$combline}'")->getField("glid,number,inventory,goods_gid",true);
		// p($gl_data);
		if (count($gl_data) == 0 && !$gl_data) {
			// echo 1;
			$data = array(
				'stat' => 0,
				'info' => ':( 没有库存,请重选规格。'
				); 
			$this->ajax($data);
		}else{
			foreach ($gl_data as $key => $value) {
				$gl_data = $value;
			}
			// p($gl_data);
			// 商品定价
			$goods_price = K('Goods')->where("gid='{$gl_data['goods_gid']}'")->getField('goods_price',true);
			foreach (explode(',',$combline) as $k => $gaid) {
				$add_p_data[] = K('GoodsAttr')->where("gaid='{$gaid}'")->getField('add_price,gavalue',true);
			}
			// p($add_p_data);
			$price_sum = 0;
			foreach ($add_p_data as $k => $v) { 
				foreach ($v as $key => $value) {
					$price_sum+=$key;
					$spec_value[] = $value;
				}
			}
			$gl_data['price_sum'] = $price_sum + $goods_price[0];
			$gl_data['spec_value'] = implode(',',$spec_value);

			// p($gl_data);
			if ($gl_data['inventory'] == 0 ) {
				$this->ajax(array(
					'stat' =>0,
					'info' =>':( 没有库存,请重选规格！'
					));
				die;
			}
			$this->ajax(array(
				'stat' =>1,
				'info' =>$gl_data
				));
		}
	}

	/**
	 * 异步添加到购物车
	 */
	public function add_cart(){
		// p(Q('post.'));
		$combline = Q('post.combline');
		$combline_data = explode(',',$combline);
		// p($combline_data);die;
		if ($combline_data[0] == 'undefined' && $combline_data[1] == 'undefined' ) {
			$this->ajax(array(
				'stat' => 0,
				'info' =>'请选择颜色和尺码规格。。。'
				));
			die;
		}

		if ($combline_data[1] == 'undefined' ) {
			$this->ajax(array(
				'stat' => 0,
				'info' =>'请选择尺码规格。。。'
				));
			die;
		}

		if ($combline_data[0] == $combline_data[1] ) {
			$this->ajax(array(
				'stat' => 0,
				'info' =>'请选择颜色规格。。。'
				));
			die;
		}
		$gl_data = M('goods_list')->where(array('combline'=>$combline))->find();
		// p($gl_data);
		$glid = $gl_data['glid'];
		$gid = $gl_data['goods_gid'];
		$g_data = M('goods')->where(array('gid'=>$gid))->getField('gid,gname,pic',true);
		foreach ($g_data as $key => $value) {
			$g_data = $value;
		}
		// p($g_data);
    	// 购买数量
		$g_number = Q('post.g_number','1','intval');
		// 商品单价
		$g_price = Q('post.g_price','','intval');
		// 商品规格
		$spec_data = M('goods_attr')->where("gaid IN ({$combline})")->getField('gavalue',true);

		$data = array(
				'id' => $gid,// 商品ID
				'glid' => $glid,// 货品ID
				'name'=>$g_data['gname'],// 商品名称
				'num' => $g_number, // 商品数量
				'price' => $g_price, // 商品价格
				'pic'=>$g_data['pic'],// 商品列表图
				'options' => array(// 其他参数如价格、颜色、可以为数组或字符串
					'color' => $spec_data[0],
					'size' => $spec_data[1]
					));
		// p($data);die;
		cart::add($data); // 添加到购物车
		
		// p($_SESSION['cart']);die;
		if ($_SESSION['cart']['goods']) {
			// p($_SESSION['cart']);
			// 根据货品id判断已加入购物车的商品是否在session中存在
			// $_SESSION['cart'][] = $gl_data['glid'];
			// p($_SESSION['cart']);
				$this->ajax(array(
					'stat' => 1,
					// 商品数量
					'total_rows' =>$_SESSION['cart']['total_rows'],
					// 商品总价
					'total' =>$_SESSION['cart']['total'],

					'info' =>'添加成功！'
				));

			}





	}





	
}
















 ?>